package com.tusuo.stds.ui.scenesearch;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hannesdorfmann.mosby.mvp.MvpPresenter;
import com.hannesdorfmann.mosby.mvp.MvpView;
import com.tusuo.stds.R;
import com.tusuo.stds.rest.SceneList;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.scenedetail.SceneDetailActivity_;
import com.tusuo.stds.ui.scenesetting.NewOnlineActivity;
import com.tusuo.stds.ui.scenesetting.SceneSettingActivity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

import rx.functions.Action0;

/**
 * Created by developer on 17-8-29.
 */

@EActivity(R.layout.activity_scene_search)
public class SceneSearchActivity extends BaseToolbarActivity<SceneSearchView,SceneSearchPresenter> implements SceneSearchView{

    @ViewById(R.id.scene_search_et_search)
    EditText etSearch;
    @ViewById(R.id.scene_search_tv_cancel)
    TextView tvCancel;
    @ViewById(R.id.scene_search_tv_count)
    TextView tvCount;
    @ViewById(R.id.scene_search_list)
    ListView mListView;

    @ViewById(R.id.scene_search_layout_default)
    LinearLayout layoutDefault;
    @ViewById(R.id.scene_search_layout_empty)
    LinearLayout layoutEmpty;
    @ViewById(R.id.scene_search_layout_result)
    LinearLayout layoutResult;

    private SceneSearchPresenter mPresenter;
    private List<SceneList.Scene> mData;
    private SceneSearchAdapter adapter;


    @NonNull
    @Override
    public SceneSearchPresenter createPresenter() {
        mPresenter = new SceneSearchPresenter(this);
        return mPresenter;
    }

    @AfterViews
    void initData() {

        etSearch.addTextChangedListener(mTextWatcher);

        mListView.setOverScrollMode(View.OVER_SCROLL_NEVER);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(SceneSearchActivity.this, SceneDetailActivity_.class);
                intent.putExtra(SceneList.Scene.EXTRA_NAME, Parcels.wrap(mData.get(position)));
                startActivity(intent);
            }
        });
    }

    @Override
    public void showSearchResult(List<SceneList.Scene> data) {

        if(data == null || data.size() == 0) {
            layoutEmpty.setVisibility(View.VISIBLE);
            layoutDefault.setVisibility(View.GONE);
            layoutResult.setVisibility(View.GONE);
        }else {
            layoutEmpty.setVisibility(View.GONE);
            layoutDefault.setVisibility(View.GONE);
            layoutResult.setVisibility(View.VISIBLE);
            tvCount.setText("为你找到" + data.size() + "个相关场景");
            mData = data;
            adapter = new SceneSearchAdapter(mData);
            mListView.setAdapter(adapter);


        }

    }

    @Click(R.id.scene_search_tv_cancel)
    void cancel() {
        finish();
    }


    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(etSearch.getText().toString().trim())) {
                mPresenter.getSearchResult(etSearch.getText().toString().trim());
            }else {
                layoutEmpty.setVisibility(View.GONE);
                layoutDefault.setVisibility(View.VISIBLE);
                layoutResult.setVisibility(View.GONE);
            }
        }
    };



    private class SceneSearchAdapter extends BaseAdapter {
        private List<SceneList.Scene> data;

        SceneSearchAdapter(List<SceneList.Scene> data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            if (data == null) {
                return 0;
            }
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view;
            ViewHolder holder;

            if (convertView == null) {
                view = View.inflate(SceneSearchActivity.this,R.layout.item_scene_search,null);
                holder = new ViewHolder();
                holder.tvTitle = (TextView) view.findViewById(R.id.item_scene_search_tv_title);
                holder.tvDescriber = (TextView) view.findViewById(R.id.item_scene_search_tv_describer);
                view.setTag(holder);
            }else {
                view = convertView;
                holder = (ViewHolder) view.getTag();
            }

            holder.tvTitle.setText(data.get(position).Title);
            holder.tvDescriber.setText(data.get(position).Description);

            return view;
        }

        class ViewHolder {
            TextView tvTitle;
            TextView tvDescriber;
        }

    }


}
